﻿var moveToBottom = true;
var Task = function (enteredTask) {
    this.timeStarted = new Date();
    this.timeStartedString = null;
    this.timeFinished = null;
    this.enteredTask = enteredTask;
    this.timeToComplete = null;
    this.isDone = false;

    this.markFinished = function () {
        this.timeFinished = new Date();
        var msec = this.timeFinished - this.timeStarted;
        var hh = Math.floor(msec / 1000 / 60 / 60);
        msec -= hh * 1000 * 60 * 60;
        var mm = Math.floor(msec / 1000 / 60);
        msec -= mm * 1000 * 60;
        var ss = Math.floor(msec / 1000);
        msec -= ss * 1000;
        this.timeToComplete = "Time To Complete: " + hh + "hours " + mm + "minutes " + ss + "seconds";
    };

    this.makeTimeStrings = function () {
        var hourNow = this.timeStarted.getHours();
        var minNow = this.timeStarted.getMinutes();
        if (minNow < 10) {
            this.timeStartedString = "Time Created: " + hourNow + ":0" + minNow;
        } else {
            this.timeStartedString = "Time Created: " + hourNow + ":" + minNow;
        }
    }
};

var taskArray = [];
var addTask = function () {
    //add task from modal to list and add to array
    var aNewTask = new Task(document.getElementById("enteredTask").value);
    taskArray.push(aNewTask);
    taskArray[taskArray.length - 1].makeTimeStrings();
    //clear text box in modal
    document.getElementById("enteredTask").value = "";
    //print to screen
    writeTasks();
};
document.getElementById("addTaskButton").onclick = addTask;

var finishedTask = function (i) {
    //strikethrough or change color of finished task
    taskArray[i].isDone = !taskArray[i].isDone;
    taskArray[i].markFinished();
    writeTasks();
};

var writeTasks = function () {
    document.getElementById("addItems").innerHTML = "";
    for (var i in taskArray) {
        if (taskArray[i].isDone == true) {
            document.getElementById("addItems").innerHTML += "<li class='list-group-item col-xs-12 listDoneTrue'><input type='checkbox'  checked class='checkbox col-xs-1'onchange='finishedTask(" + (i) + ");'><span class='col-xs-8'>" + taskArray[i].enteredTask + "</span><span class='col-xs-2'>" + taskArray[i].timeToComplete + "</span></li></div>";
        } else {
            document.getElementById("addItems").innerHTML += "<li class='list-group-item col-xs-12 listDoneFalse'><input type='checkbox' class='checkbox col-xs-1'onchange='finishedTask(" + (i) + ");'><span class='col-xs-8'>" + taskArray[i].enteredTask + "</span><span class='col-xs-2'>" + taskArray[i].timeStartedString + "</li></div>";
        }
    }
};

var clearCheckedElements = function () {
    for (var i in taskArray) {
        if (taskArray[i].isDone === true) {
            taskArray.splice(i, 1);
            clearCheckedElements();
        }
    }
    writeTasks();
};
document.getElementById("clearFinished").onclick = clearCheckedElements;

var moveCheckedToBottom = function () {
    var lastIndex = taskArray.length;
    var temp;
    if (moveToBottom) {
        for (var i = taskArray.length - 1; i >= 0; i--) {
            if (taskArray[i].isDone) {
                taskArray.push(taskArray.splice(i, 1).pop());
            }
        }
        document.getElementById("moveFinishedToBot").innerHTML = "Move finished to top";
    }
    else {
        for (var i = 0; i < lastIndex; i++) {
            if (taskArray[i].isDone) {
                taskArray.unshift(taskArray.splice(i, 1).pop());
            }
        }
        document.getElementById("moveFinishedToBot").innerHTML = "Move finished to bottom";
    }
    moveToBottom = !moveToBottom

    writeTasks();
};
document.getElementById("moveFinishedToBot").onclick = moveCheckedToBottom;
